import requests
import json

from dataop import config


class JianDaoYunAPI:
    jdy_api_conf = config.Config("config", "project_info.conf").get_config()

    Authorization = jdy_api_conf.get("headers", "Authorization")
    Content_Type = jdy_api_conf.get("headers", "Content-Type")
    headers = {'Authorization': Authorization, 'Content-Type': Content_Type}

    def __init__(self):
        None

    # param json formatter
    # {
    #     "app_id": "59264073a2a60c0c08e20bfb",
    #     "entry_id": "59264073a2a60c0c08e20bfd",
    #     "data_id": "",
    #     "fields": [],
    #     "filter": {},
    #     "limit": 100
    # }

    def requestDatas(self, url,  params):
        response = requests.request("POST", url, headers=self.headers, data=params)
        data = response.text
        return json.loads(data)

    # 查询用户app应用的app_id
    def get_AppId(self, module_name):
        apiurl = self.jdy_api_conf.get("jiandaoyun_apiurl", "usersAppList_apiurl")
        data = self.requestDatas(apiurl,  None)
        app_id = None
        for app in data["apps"]:
            if app["name"] == module_name:
                app_id = app["app_id"]
                break
        return app_id

    # 查询应用下的对应表单id
    def getEntryId(self,  module_name, tablename):
        apiurl = self.jdy_api_conf.get("jiandaoyun_apiurl", "tableDtlInfo_apiurl_v5")
        # print(apiurl)
        app_id = self.get_AppId( module_name)
        paramData = {"app_id": app_id, "skip": 0, "limit": 0}
        data = self.requestDatas(apiurl,  json.dumps(paramData))
        # print(data)
        entry_id = None
        for form in data["forms"]:
            if form["name"] == tablename:
                entry_id = form["entry_id"]
        return entry_id

    # 查询表结构
    def getEntryFields(self,  module_name, tablename):
        apiurl = self.jdy_api_conf.get("jiandaoyun_apiurl", "usersAllTablesInfoOnCurApp_apiurl")
        # print(apiurl)
        app_id = self.get_AppId( module_name)
        entry_id = self.getEntryId( module_name, tablename)
        paramData = {"app_id": app_id, "entry_id": entry_id}
        data = self.requestDatas(apiurl, json.dumps(paramData))
        # print(data)
        field_list = []
        for widget in data["widgets"]:
            field_list.append(widget["name"])
        for sysWidget in data["sysWidgets"]:
            field_list.append(sysWidget["name"])
        return field_list

    # 查询表单数据
    def getTableDatas(self,  module_name, tablename):
        apiurl = self.jdy_api_conf.get("jiandaoyun_apiurl", "mulit_data_query_url")
        fields_list = self.getEntryFields( module_name, tablename)
        app_id = self.get_AppId(module_name)
        entry_id = self.getEntryId(module_name, tablename)
        paramData = {"app_id": app_id, "entry_id": entry_id, "fields": fields_list, "filter": [], "limit": 100}
        data = self.requestDatas(apiurl, json.dumps(paramData))
        return data

    @classmethod
    def get_apidata(cls, param_data):
        url = cls.get_apiurl()
        param_data = json.loads(param_data)


if __name__ == "__main__":
    jdy = JianDaoYunAPI()
    print(jdy.getTableDatas('劳务结算板块', '项目信息填报表'))